package object

import (
	"time"

	v1 "code.byted.org/bytertc/whiteboard_server/biz/api/pb/v1"
	"code.byted.org/bytertc/whiteboard_server/biz/pkg/biz_error"
	"github.com/pkg/errors"
	"google.golang.org/protobuf/proto"
)

func NewBoard(appId, roomId string, info *v1.BoardInfo, updatedAt time.Time) (Board, error) {
	return &board{
		AppId:     appId,
		RoomId:    roomId,
		BoardInfo: info,
		UpdatedAt: updatedAt,
	}, nil
}

type board struct {
	AppId     string
	RoomId    string
	UpdatedAt time.Time
	*v1.BoardInfo
}

func (b *board) AddActionCount(newPhases int32) {
}

func (b *board) GetActionCount() int32 {
	totalCount := int32(0)
	for _, count := range b.ControlCount {
		totalCount += count
	}
	return totalCount
}

func (b *board) GetAppId() string {
	return b.AppId
}

func (b *board) GetRoomId() string {
	return b.RoomId
}
func (b *board) GetUpdateAt() time.Time {
	return b.UpdatedAt
}

func (b *board) Set(another ProtoObject) {
	if v, ok := another.(*v1.BoardInfo); ok {
		b.BoardInfo = v
	}
}
func (b *board) SetInitStatus(finished bool) {

}
func (b *board) GetInitStatus() bool {
	return true
}

type Page struct {
	ControlCount int32
	*v1.PageInfo
}

func NewPage(count int32, rawData []byte) (*Page, error) {
	info := &v1.PageInfo{}
	err := proto.Unmarshal(rawData, info)
	return &Page{
		ControlCount: count,
		PageInfo:     info,
	}, biz_error.DecodeError.Wrap(errors.WithStack(err))
}
